/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-10 16:25:31
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-11-04 20:07:22
 * @FilePath: \实训二——宏业找房\egg\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const { Controller } = require('egg');
class BulkController extends Controller {
    // 获取团购数据
    async getBulk() {
        const { ctx } = this;
        const results = await this.app.mysql.select('bulk');
        const { one, two, three, four, pageCode, pageSize } = ctx.request.body;
        let newLists = results;
        if (one) {
            newLists = newLists.filter(v => v.address.includes(one))
        }
        if (two) {
            newLists = newLists.filter(v => v.address.includes(two))
        }
        if (three) {
            newLists = newLists.filter(v => v.address.includes(three))
        }
        if (four) {
            newLists = newLists.filter(v => v.name.includes(four))
        }
        
        let total = newLists.length;
        newLists = newLists.slice((pageCode * 1 - 1) * pageSize, pageCode * pageSize)
        if (results) {
            ctx.body = {
                code: 200,
                list: newLists,
                total,
                msg: '获取成功'
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '获取失败'
            }
        }
    }
    // 删除数据
    async delBulk() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('bulk', { id });
        if (result) {
            const results = await this.app.mysql.select('bulk');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '删除失败'
            }
        }

    }
    // 状态的改变
    async changeBulkStatus() {
        const { ctx } = this;
        const { id, status } = ctx.request.body;
        // 关闭
        if (status === 'true') {
            const row = {
                status: 'false'
            };

            const options = {
                where: {
                    id
                }
            };
            const result = await this.app.mysql.update('bulk', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '关闭成功',
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '关闭失败'
                }
            }
        } else if (status === 'false') {
            // 开启
            const row = {
                status: 'true'
            };

            const options = {
                where: {
                    id
                }
            };
            const result = await this.app.mysql.update('bulk', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '开启成功',
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '开启失败'
                }
            }
        }
    } 

    // 经纪人团购数据
    async agentBulk() {
        const { ctx } = this;
        const results = await this.app.mysql.select('agentBulk');
        if (results) {
            ctx.body = {
                code: 200,
                list: results,
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '获取失败'
            }
        }
    }


}

module.exports = BulkController;
